﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

namespace Battle
{
    public class BCUIParticleTransform : MonoBehaviour
    {
        public View.BattleCharacter ownBC;
        public string bindBoneName;
        public Transform bindTransform = null;


        public void Init(View.BattleCharacter bc, string bindName, bool isFocus = true)
        {
            ownBC = bc;
            bindBoneName = bindName;
            bindTransform = ownBC.model.GetComponent<ModelRenderKit>().GetBindTransform(bindName);

            lastPos = Vector3.zero;
            Update();
        }

        Vector3 lastPos = Vector3.zero;


        void Update()
        {

            if (bindTransform != null)
            {
                Vector3 pos = bindTransform.position;
                pos = View.BattleViewCtrl.GetInstance().battleCamera.WorldToScreenPoint(pos);
                pos = UICamera.currentCamera.ScreenToWorldPoint(pos);
                pos.z = 0f;

                if (lastPos == Vector3.zero)
                {
                    lastPos = pos;
                }
                else
                {
                    if (Vector3.Distance(lastPos, pos) > 10)
                    {
                        lastPos = pos;
                    }
                }

                transform.position = lastPos;
                transform.localScale = Vector3.one;
                transform.localEulerAngles = Vector3.zero;
            }

        }


        public void PlayParticle(ParticleObj obj, bool isAutoRecovery)
        {
            obj.Play(LayerName.UI, transform, Vector3.zero, 1f, Vector3.zero, isAutoRecovery);
        }


        void Destroy()
        {
            foreach (Transform trans in transform)
            {
                Battle.View.BattleFactory.UnSpawn(trans.gameObject);
            }
        }
    }

}